<template>
	<view style="background-color: #F0F2F5;width: 100vw;height: 100vh;">

		<view
			class="class">

			<image src="../../static/header.png" style="width: 88px;height: 88px;margin-left: 15px;"></image>

			<view style="display: flex;flex-direction: column;color: white;font-size: 16px;margin-left: 10px;">
				<text>{{userName}}</text>
				<text style="margin-top: 5px;">{{userPhone}}</text>
			</view>
		</view>

		<view style=" border-radius: 10px;background-color: white;margin: 0px 15px 0px 15px;">
			<text
				style="width: 100%;height: 35px;font-size: 16px;color: #1A1A1A;margin-left: 10px;display: flex;align-items: center;">系统设置</text>
			<view style="display: flex;flex-direction: row;height: 55px;align-items: center;margin-left: 15px;"
				v-for="item in systemSetting" @click="onItemClick(item.data,item.name)">
				<image :src="item.icon" style="width: 30px;height: 30px;"></image>
				<text style="color: #333333;font-size: 15px;margin-left: 6px;">{{item.name}}</text>
				<text v-if="item.name == '版本'" >{{appVersion}}</text>
			</view>
		</view>

		<view
			style="display: flex;flex-direction: row;height: 55px;align-items: center; border-radius: 10px;background-color: white;margin: 20px 15px 0px 15px;" @click="logOff">
			<image src="../../static/logoff.png" style="width: 30px;height: 30px;margin-left: 15px;"></image>
			<text style="color: #333333;font-size: 15px;margin-left: 6px;" >退出登录</text>
		</view>
		<suspended></suspended>
	</view>
</template>

<script>
	import {
		SetwriteBLECharacteristicValue,
		hexCharCodeToStr,
		ab2hex,
	} from '../../comment/dataUtils.js'
	export default {
		data() {
			return {
				userName: '陆离',
				userPhone: '1888888888',
				systemSetting: [{
					name: '系统复位',
					icon: '../../static/reset_system.png',
					data: '$RST'
				}, {
					name: '恢复出厂',
					icon: '../../static/resume_the_factory.png',
					data: '$FRST'
				}, {
					name: '清除存储',
					icon: '../../static/clear_the_storage.png',
					data: '$SDC'
				}, {
					name: '版本',
					icon: '../../static/app_name.png',
					data: ''
				}],
				sendDataName: '',
				appVersion:''
			}
		},

		onLoad() {
			
			const accountInfo = wx.getAccountInfoSync();
			this.appVersion = accountInfo.miniProgram.version
			
		//	this.appVersion = plus.runtime.version
			console.log('版本号',this.appVersion)
			uni.request({
				url: this.$baseURL + '/auth/info',
				data: {},
				method: "GET",
				header: {
					 //自定义请求头信息
					'Authorization':uni.getStorageSync('token')
				},
				success: (res) => {
					uni.hideLoading()
					console.log("请求成功", res.data);
					if (res.data.code == 200) {
						this.userName = res.data.data.user.nickName
						this.userPhone = res.data.data.user.phone
					} else if (res.data.code == 500) {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					}
				},

				fail: (res) => {
					uni.hideLoading()
					console.log(res);
				}
			});
		},

		methods: {
			onItemClick(data, name) {
				this.SetListenMessage()
				if(name == "版本") {
					return
				}
				if (getApp().globalData.deviceId != '') {
					uni.showModal({
						title: '提示',
						content: '确定要' + name + '吗？',
						cancelText: "取消", // 取消按钮的文字  
						confirmText: "确认", // 确认按钮的文字  
						showCancel: true, // 是否显示取消按钮，默认为 true
						confirmColor: '#f55850',
						cancelColor: '#39B54A',
						success: (res) => {
							if (res.confirm) {
								this.sendDataName = data + ";"
								SetwriteBLECharacteristicValue(data)
							} else {
								console.log('cancel') //点击取消之后执行的代码
							}
						}
					})
				} else {
					uni.showToast({
						title: '请连接蓝牙设备',
						icon: 'error'
					})
				}
			},

			//设置监听发送过来的消息
			SetListenMessage() {
				let that = this
				let result = ''

				uni.onBLECharacteristicValueChange((res) => {
					let resHex = ab2hex(res.value)
					result = hexCharCodeToStr(resHex).replace(/\ +/g, "").replace(/[ ]/g, "").replace(/[\r\n]/g,
						"")
					console.log('设置蓝牙返回的数据', result)
					switch (result) {
						case '$RST;':
							uni.showToast({
								title: '系统复位成功',
								icon: 'success'
							})
							break
						case '$FRST;':
							uni.showToast({
								title: '恢复出厂成功',
								icon: 'success'
							})
							break
						case '$SDC;':
							uni.showToast({
								title: '清除存储成功',
								icon: 'success'
							})
							break
						default:

							break

					}
				})
			},

			logOff() {
				uni.showModal({
					title: '提示',
					content: '确定要退出账号吗？',
					cancelText: "取消", // 取消按钮的文字  
					confirmText: "确认", // 确认按钮的文字  
					showCancel: true, // 是否显示取消按钮，默认为 true
					confirmColor: '#f55850',
					cancelColor: '#39B54A',
					success: (res) => {
						if (res.confirm) {
							uni.clearStorageSync()
							uni.reLaunch({
								url: '/pages/login/login'
							})
						} else {
							console.log('cancel') //点击取消之后执行的代码
						}
					}
				})
			}
		}
	}
</script>

<style>
	.class {
		width: 100%;
		height: 258px;
		background-size: 100% 100%;
		display: flex;
		align-items: center;
		background-image:url(''); 	
	}
</style>
